package org.lwx.reflect.constructor;

import org.lwx.annotation.target.CONSTRUCTOR;
import org.lwx.reflect.pojo.User;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @author wx-li
 * @company 希睿科技(www.xraitech.com)
 * @copyright (c) 2018 希睿科技( www.xraitech.com )Inc. All rights reserved.
 * @date 2020/10/15 10:39
 * @since JDK1.8
 */
public class ConstructorTest {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Class<User> aClass = (Class<User>) Class.forName("org.lwx.reflect.pojo.User");
//        Constructor<User> declaredConstructor = aClass.getDeclaredConstructor();
//        Constructor<User> declaredConstructor1 = aClass.getDeclaredConstructor(String.class, Integer.class);
//        declaredConstructor.setAccessible(true);
//        User user = declaredConstructor.newInstance();
//        declaredConstructor1.setAccessible(true);
//        User user1 = declaredConstructor1.newInstance("wx-li", 24);

        for (Constructor<?> declaredConstructor : aClass.getDeclaredConstructors()) {
            if (declaredConstructor.isAnnotationPresent(CONSTRUCTOR.class)) {
                declaredConstructor.setAccessible(true);
                Class<?>[] parameterTypes = declaredConstructor.getParameterTypes();
                Object[] param = new Object[parameterTypes.length];
                for (int i = 0; i < parameterTypes.length; i++) {
                    param[i] = parameterTypes[i].newInstance();
                }
                Object o = declaredConstructor.newInstance(param);
                System.out.println(o);
            }
        }


//        Constructor<User> constructor = aClass.getConstructor();
//        Constructor<User> constructor1 = aClass.getConstructor(String.class, Integer.class);
//        Constructor<?>[] constructors = aClass.getConstructors();
//        Constructor<?>[] declaredConstructors = aClass.getDeclaredConstructors();
//        User user = constructor.newInstance();
//        User user1 = constructor1.newInstance("wx-li", 24);
        System.out.println();
    }
}
